<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="游戏名称">
          <el-input v-model="form.name" placeholder="游戏名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column
            prop="id"
            label="游戏编号"
            align="center"
            width="100"
          />

          <el-table-column
            prop="name"
            label="游戏名称"
            align="center"
            width="150"
          />
          <el-table-column label="封面" align="center" width="110">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.cover"
                alt=""
                fit="fill"
                :preview-src-list="[scope.row.cover]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="游戏icon" align="center" width="110">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.icon"
                alt=""
                fit="fill"
                :preview-src-list="[scope.row.icon]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="背景图片" align="center" width="110">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.img"
                alt=""
                fit="fill"
                :preview-src-list="[scope.row.img]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column
            prop="h5_play_url"
            label="H5网址"
            align="center"
            width="240"
          />
          <el-table-column
            prop="slogan"
            label="Slogan"
            align="center"
            width="240"
          />
          <el-table-column
            prop="version"
            label="游戏版本"
            align="center"
            width="110"
          />
          <el-table-column
            label="操作"
            align="center"
            width="240"
            fixed="right"
          >
            <template #default="scope">
              <div class="flex">
                <el-button
                  type="primary"
                  plain
                  @click="handleOperate(scope.$index, scope.row, 'details')"
                  >详情</el-button
                >
                <el-button
                  type="primary"
                  plain
                  @click="handleOperate(scope.$index, scope.row, 'edit')"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  @click="handleOperate(scope.$index, scope.row, 'delete')"
                  >删除</el-button
                >
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DetailsForm
      @cancel="detailsCancel"
      @closeDialog="detailsCloseDialog"
      :dialogFormVisible="detailsVisible"
      :dialogForm="detailsForm"
    />
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      @handleUploaderImgRequest="handleUploader"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportToExcel } from "@/utils/utils";
import {
  gameListApi,
  gameDelApi,
  gameInfoApi,
  gameAddApi,
  gameUpdateApi,
} from "@/api/moby/chainGame/chainGame";
import DetailsForm from "./components/detailsForm.vue";
import DialogForm from "./components/dialogForm.vue";
const tableData = ref([]);
const loading = ref(false);
const detailsVisible = ref(false);
const detailsForm = reactive({});
const dialogVisible = ref(false);
const btnLoading = ref(false);
const btnType = ref(null);
const formData = {
  name: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  name: "",
  score: "",
  img: "",
  img_show: "",
  cover: "",
  cover_show: "",
  icon: "",
  icon_show: "",
  h5_play_url: "",
  slogan: "",
  version: "",
  version_log: "",
  desc: "",
  h5_download_url: "",
  download_url: "",
  ios_download_url: "",
  android_download_url: "",
  android_apk_name: "",
  ios_ipa_name: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    name: form.name,
  };
};

const handleOperate = async (index, row, type) => {
  // 详情
  if (type == "details") {
    const res = await gameInfoApi({ id: row.id });
    Object.assign(detailsForm, { ...res.data });
    detailsVisible.value = true;
  }
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    const res = await gameInfoApi({ id: row.id });
    Object.assign(dialogForm, { ...res.data });
    dialogVisible.value = true;
  }
  // 删除
  if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除此游戏?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await gameDelApi({ id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
};
// 详情弹窗关闭
const detailsCancel = () => {
  detailsVisible.value = false;

  Object.assign(detailsForm, {});
};
//  详情弹窗关闭
const detailsCloseDialog = () => {
  detailsVisible.value = false;

  Object.assign(detailsForm, {});
};

//新增
const onAdd = () => {
  dialogVisible.value = true;
  btnType.value = "add";
};

// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await gameListApi(data);

  let head = {
    id: "ID",
    name: "名称",
    cover: "封面",
    icon: "Icon",
    img: "背景图片",
    h5_play_url: "H5网址",
    slogan: "Slogan",
    version: "游戏版本",
    createtime: "添加时间",
  };
  exportToExcel(res.data.results, head, "Mobybox-链游管理");
};

//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  // console.log(param);
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id;
      const res = await gameAddApi(param);

      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    if (btnType.value == "edit") {
      // console.log(param);
      const res = await gameUpdateApi(param);

      ElMessage({
        message: res.message,
        type: "success",
      });
    }

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
// //上传图标
const handleUploader = (res, name, src) => {
  // console.log('----')
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });

    dialogForm[src] = res.data.pic_url || "";
    dialogForm[name] = res.data.name || "";
  }
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await gameListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
